﻿using BacklogParser.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BacklogParser.Parsers
{
    public static class OppositionDataParser
    {
        public static List<OpositionData> Parse(List<XElement> elems)
        {
            var result = new List<OpositionData>();

            foreach (var item in elems)
            {
                var opponents = item.Descendants(Constants.Epo + "opponent").ToList();
                var dateRejected = item.Descendants(Constants.Epo + "date-rejected").FirstOrDefault();
                var dateTerminated = item.Descendants(Constants.Epo + "date-terminated").FirstOrDefault();
                var oppositionNotFilled = item.Descendants(Constants.Epo + "opposition-not-filed").FirstOrDefault();

                var data = new OpositionData();

                if (dateRejected != null)
                    data.DateRejected = dateRejected.Value;

                if (dateTerminated != null)
                    data.DateRejected = dateTerminated.Value;

                if (oppositionNotFilled != null)
                    data.DateRejected = oppositionNotFilled.Value;

                if (opponents != null)
                {
                    foreach (var opponentElement in opponents)
                    {
                        Opponent opponent = ParseOpponent(opponentElement);
                        data.Opponent.Add(opponent);
                    }
                }

                result.Add(data);
            }

            return result;
        }

        private static Opponent ParseOpponent(XElement opponentElement)
        {
            var addressBook = opponentElement.Descendants(Constants.Epo + "addressbook").FirstOrDefault();
            var agent = opponentElement.Descendants(Constants.Epo + "agent").FirstOrDefault();
            var opponentStatus = opponentElement.Descendants(Constants.Epo + "opponent-status").FirstOrDefault();
            var dateOppositionFiled = opponentElement.Descendants(Constants.Epo + "date-opposition-filed").FirstOrDefault();

            var result = new Opponent();

            if (addressBook != null)
            {
                AddressBook opponentParsed = AddressBookParser.Parse(addressBook);
                var idAttr = addressBook.Attribute("cdsid");

                if (idAttr != null)
                    result.CdsId = idAttr.Value;

                result.Name = opponentParsed.Name;
                result.Address1 = opponentParsed.Address1;
                result.Address2 = opponentParsed.Address2;
                result.Address3 = opponentParsed.Address3;
                result.Address4 = opponentParsed.Address4;
                result.Address5 = opponentParsed.Address5;

                result.Country = opponentParsed.Country;
            }

            if (agent != null)
            {
                AddressBook agentAdressbook = AddressBookParser.Parse(agent);
                result.Agent = Agent.Create(agentAdressbook);
            }

            if(opponentStatus != null)
                result.OpponentStatus = opponentStatus.Value;

            if (dateOppositionFiled != null)
                result.DateOppositionFiled = dateOppositionFiled.Value;

            return result;
        }

      
    }
}
